// components/tabBar/tabBar.js
var common = require("../../common/common.js").common;

const app = getApp();

const defaultConfig = {
    numShow: false,
    mainColor: "#232323",
    //主色#47B34F
    isOffTopTip: false,
    //是否开启数字上标
    fff: false,
    num: 0,
    tabBar: [],
    tabBar1: [ {
        iconName: "icon-shouye1",
        //图标名
        tip: "首页",
        //图标提示+
        current: 1,
        //是否是当前图标
        num: 0,
        //上标数字
        url: "/pages/index/index",
        openType: "switchTab"
    }, {
        iconName: "icon-fenlei1",
        //图标名
        tip: "分类",
        //图标提示
        current: 0,
        //是否是当前图标
        num: 0,
        //上标数字
        url: "/pages/classfit/classfit",
        openType: "switchTab"
    }, {
        iconName: "icon-gouwuche2",
        //图标名
        tip: "购物车",
        //图标提示
        current: 0,
        //是否是当前图标
        num: 0,
        //上标数字
        url: "/pages/shopcart/shopcart"
    }, {
        iconName: "icon-dingdan2",
        //图标名
        tip: "订单",
        //图标提示
        current: 0,
        //是否是当前图标
        num: 0,
        //上标数字
        url: "/pages/order/order",
        openType: "switchTab"
    }, {
        iconName: "icon-wode1",
        //图标名
        tip: "我的",
        //图标提示
        current: 0,
        //是否是当前图标
        num: 0,
        //上标数字
        url: "/pages/personal/personal",
        openType: "switchTab"
    } ],
    num_c: 0
};

Component({
    externalClasses: [ "m-class" ],
    options: {
        addGlobalClass: true
    },
    /**
   * 组件的属性列表
   */
    pageLifetimes: {
        show: function() {
            // 页面被展示
        },
        hide: function() {
            // 页面被隐藏
            this.setData({
                numShow: false
            });
        },
        resize: function(size) {
            // 页面尺寸变化
        }
    },
    properties: {
        numShow: {
            type: Boolean,
            observer(newVal, oldVal, changePath) {
                this.setData({
                    numShow: newVal
                });
            }
        },
        tabBarConfig: {
            type: Object,
            observer(newVal, oldVal, changePath) {
                this.setData({
                    tabBar: newVal
                });
            }
        },
        num: {
            type: Number,
            value: 0,
            observer(newVal, oldVal, changePath) {
                this.setData({
                    num: newVal
                });
                this.getBase();
            }
        },
        mainColorConfig: {
            type: String,
            observer(newVal, oldVal, changePath) {
                this.setData({
                    mainColor: newVal
                });
            }
        },
        offTopTip: {
            type: String,
            observer(newVal, oldVal, changePath) {
                this.setData({
                    isOffTopTip: newVal
                });
            }
        },
        tabBarStatus: {
            type: Number,
            observer(newVal, oldVal, changePath) {
                this.setData({
                    tabBarStatus: newVal
                });
            }
        }
    },
    /**
   * 组件的初始数据
   */
    data: {
        ...defaultConfig
    },
    /**
   * 组件的方法列表
   */
    methods: {
        gotoBottomUrl: function(e) {
            let _this = this;
            var type = e.currentTarget.dataset.type;
            var url = e.currentTarget.dataset.url;
            //跳转小程序
                        if (url.indexOf("appid") != -1) {
                var arr = url.split("&");
                var appid = arr[0].split("=")[1];
                var page = arr[1].split("=")[1];
                wx.navigateToMiniProgram({
                    appId: appid,
                    path: page,
                    success(res) {
                        // 打开成功
                    }
                });
            }
            if (url.indexOf("phone") != -1) {
                var arr = url.split("=");
                wx.makePhoneCall({
                    phoneNumber: arr[1]
                });
                return;
            }
            _this.getBase();
            _this.cart_num().then(() => {
                if (type == "navigate") {
                    if (url == "/pages/shopcart/shopcart") {
                        let page = getCurrentPages().pop();
                        if ((!page.data.myleaderinfo.leader_id || page.data.myleaderinfo.leader_id == -1) && _this.data.defaultGoods.length) {
                            wx.showModal({
                                title: "提示",
                                content: "您还没有选择" + page.data.leaderName + "，请先选择" + page.data.leaderName + "后再下单",
                                showCancel: false,
                                success(res) {
                                    if (res.confirm) {
                                        wx.navigateTo({
                                            url: "/pages/nearby_group2/nearby_group2"
                                        });
                                    }
                                }
                            });
                        } else {
                            wx.navigateTo({
                                url: url
                            });
                        }
                    } else {
                        wx.navigateTo({
                            url: url
                        });
                    }
                } else {
                    if (url.indexOf("/pages/classfit/classfit") >= 0) {
                        app.globalData.link = url;
                        url = "/pages/classfit/classfit";
                    }
                    wx.switchTab({
                        url: url,
                        success: function(e) {
                            let page = getCurrentPages().pop();
                            if (page == undefined || page == null) return;
                            page.onLoad();
                        }
                    });
                }
            });
        },
        getBase: function() {
            common.request({
                endpoint: "set/app_base",
                method: "get",
                data: {},
                success: res => {
                    this.setData({
                        num_c: res.extra.cart_total_num
                    });
                }
            });
        },
        cart_num: function() {
            let _this = this;
            return new Promise(function(resolve, reject) {
                let cart = wx.getStorageSync("cart") || [];
                var ids = [];
                cart.forEach(item => [ ids.push(item.id) ]);
                var param = {};
                param["data"] = ids;
                common.request({
                    endpoint: "product/cart_num",
                    method: "post",
                    data: param,
                    success(res) {
                        _this.setData({
                            expressGoods: res.extra.express || [],
                            defaultGoods: res.extra.default || []
                        });
                        resolve();
                    }
                });
            });
        }
    }
});